package com.xoms.front.persist.impl;

import com.xoms.front.aspect.datasource.DataSourceSwitcher;
import com.xoms.front.controller.res.BaseResult;
import com.xoms.front.controller.res.ObjectResult;
import com.xoms.front.controller.res.bean.*;
import com.xoms.front.controller.springmvc.req.*;
import com.xoms.front.model.*;
import com.xoms.front.persist.dao.RoomDao;
import com.xoms.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xoms.front.persist.impl.BaseImpl.NAME_SPACE_HEADER;

/**
 * Created with IntelliJ IDEA.
 * User: tzz(Tian)
 * Date: 2018/5/25 14:49
 */

@Repository
public class RoomImpl extends BaseImpl implements RoomDao {
    final String NAME_SPACE = NAME_SPACE_HEADER + ".RoomMapper";

    @Override
    public List<RoomDimensionBean> selectDimensions() {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimensions");
    }

    @Override
    public List<RRoomDimensionDetailBean> getDimensionDetailList(GetRoomDimensionDetailsParam param) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (!StringUtil.isEmpty(param.getDimensionId())) {
            params.put("dimensionId", param.getDimensionId());
        }
        if (param.getStatus() != null && param.getStatus() != -1) {
            params.put("status", param.getStatus());
        }
        if (!StringUtil.isEmpty(param.getScales())) {
            params.put("scales", param.getScales().split(","));
        }
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimensionDetails", params);
    }

    @Override
    public List<RRoomStatusListBean> getRoomStatusList(String scales) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (!StringUtil.isEmpty(scales)) {
            params.put("scales", scales.split(","));
        }
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoomStatusList", params);
    }

    @Override
    public List<RRoomListBean> getRoomList(GetRoomListParam param) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (param.getStatus() != null && param.getStatus() != -1) {
            params.put("status", param.getStatus());
        }
        if (!StringUtil.isEmpty(param.getScales())) {
            params.put("scales", param.getScales().split(","));
        }
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoomList", params);
    }

    @Override
    public List<RRoomBookInfoBean> getRoomBookList(String id) {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoomBookList", id);
    }

    @Override
    public List<RFilmListBean> getFilmList(GetFilmListParam param) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", param.getType());
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectFilmList", params);
    }

    @Override
    public List<RCardInfoBean> getCardList(String userId, int setPhoneLogin) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("setPhoneLogin", setPhoneLogin);
        // 手动切换数据源
        DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_HQ);
        List<RCardInfoBean> list = sqlSessionTemplate.selectList(NAME_SPACE + ".selectCardList", params);
        DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_XOMS);
        return list;
    }

    @Override
    public RCardInfoBean getCardInfo(String cardNo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("cardNo", cardNo);
        // 手动切换数据源
        DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_HQ);
        RCardInfoBean bean = sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCardList", params);
        DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_XOMS);
        return bean;
    }

    @Override
    public int roomLock(RoomLockParam param) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", param.getId());
        return sqlSessionTemplate.insert(NAME_SPACE + ".roomLock", params);
    }

    @Override
    public MemberBean getMember(String id, String phoneNum) {
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", id);
        params.put("phoneNum", phoneNum);
        // 手动切换数据源
        DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_HQ);
        MemberBean member = sqlSessionTemplate.selectOne(NAME_SPACE + ".selectMember", params);
        DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_XOMS);
        return member;
    }

    @Override
    public RRoomPriceBean getPrice(String id, String btime, String etime) {
        Map<String, Object> params = new HashMap<>();
        params.put("roomId", id);
        params.put("startTime", btime);
        params.put("endTime", etime);
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".getPrice", params);
    }

    @Override
    public int createOrder(OrderBean order, RoomOpenBean open, RoomOpenAddBean add) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("order", order);
        params.put("open", open);
        params.put("add", add);
        return sqlSessionTemplate.insert(NAME_SPACE + ".createOrder", params);
    }

    @Override
    public OrderBean selectRoomOrder(String id, String roomId, String OrderNo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("roomId", roomId);
        params.put("OrderNo", OrderNo);
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectRoomOrder", params);
    }

    @Override
    public AccountBean selectAccountByTel(String phoneNum) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectAccount", phoneNum);
    }

    @Override
    public int setOrderAuthPrice(EditOrderAuthPriceParam param, String accId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("authMoney", param.getAuthMoney());
        params.put("authAccountId", accId);
        params.put("oid", param.getId());
        return sqlSessionTemplate.insert(NAME_SPACE + ".setOrderAuthPrice", params);
    }

    @Override
    public int roomOpen(RoomOpenParam param) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("roomId", param.getId());
        params.put("oid", param.getOid());
        return sqlSessionTemplate.insert(NAME_SPACE + ".roomOpen", params);
    }

    @Override
    public int orderPayWay(RoomOrderPayWayParam param, int orderType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderType", orderType);
        params.put("id", param.getId());
        params.put("payWay", param.getPayWay());
        params.put("getMoney", param.getGetMoney());
        params.put("isHand", param.getIsHand());
        params.put("handRoomId", param.getHandRoomId());
        return sqlSessionTemplate.insert(NAME_SPACE + ".orderPayWay", params);
    }

    @Override
    public RRoomInfoBean selectRoomById(String id) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectRoomById", id);
    }

    @Override
    public RoomOpenBean getRoomOpenOrder(String orderCode) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderCode", orderCode);
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCurrentOpenOrder", params);
    }

    @Override
    public OrderBean getCurrentOrder(String id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("roomId", id);
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectRoomCurrentOrder", params);
    }
}
